#include "ScriptComp.h"
#include <SEngine/Script/StateLoader.h>
#include <SEngine/Ecs/EntityLoader/ActorLoader.h>
#include <SEDebug/Log/Log.h>
#include <SECore/Manager.h>
#include <SEngine/Ecs/TypeRegister.h>

namespace SEngine
{
    void ScriptComp::Attach()
    {
    }
    
    void ScriptComp::BeforeUpdate()
    {
        if (lua) {
            lua.Value().BeforeUpdate();
        }
    }
    
    void ScriptComp::OnUpdate(int delta)
    {
        if (lua) {
            lua.Value().OnUpdate(delta);
        }
    }
    
    void ScriptComp::AfterUpdate()
    {
        if (lua) {
            lua.Value().AfterUpdate();
        }
    }
    void ScriptComp::Load(const std::string &path)
    {
        lua = Manager::GetInstance().Get<ScriptState>(path).value();
        lua.Value().Attach(m_actor);
    }
    
    void ScriptComp::Reload()
    {
        if (lua) {
            lua.Reload([&](auto & path, auto & name) {
                auto state = std::make_unique<ScriptState>(path);
                state->Attach(m_actor);
                return state;
            });
        }
    }
} // namespace SEngine
